﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AnalizadorCsv.Clases;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

namespace AnalizadorCsv.Configuracion
{
    public partial class ConfiguracionOpciones : Form
    {
        public ConfiguracionOpciones()
        {
            InitializeComponent();
            cargarCampos();
        }
        private void cargarCampos(String archivo, DataGridView datagrid)
        {
            Campos cam = new Campos();
            XmlSerializer serial = new XmlSerializer(typeof(Campos));
            XmlTextReader xt = new XmlTextReader(archivo);

            cam = (Campos)serial.Deserialize(xt);
            xt.Close();
            CamposConfiguracion[] campos = cam.campoDetalle;
            datagrid.RowCount = campos.Length;
            for (int i = 0; i < campos.Length; i++)
            {
                datagrid.Rows[i].Cells[0].Value = campos[i].nombreCampo;
                datagrid.Rows[i].Cells[1].Value = campos[i].noBlank;
                datagrid.Rows[i].Cells[2].Value = campos[i].tipo;
                datagrid.Rows[i].Cells[3].Value = campos[i].reemplazar;
                datagrid.Rows[i].Cells[4].Value = campos[i].textoReemplazo;

            }
        }
        private Campos obtenerCampos(DataGridView datagrid)
        {
            List<CamposConfiguracion> campos = new List<CamposConfiguracion>();
            for (int i = 0; i < datagrid.RowCount; i++)
            {
                CamposConfiguracion campo = new CamposConfiguracion();
                campo.setTipo(datagrid.Rows[i].Cells[2].Value.ToString());
                campo.nombreCampo = datagrid.Rows[i].Cells[0].Value.ToString();
                campo.noBlank = (bool)datagrid.Rows[i].Cells[1].Value;
                campo.reemplazar = (bool)datagrid.Rows[i].Cells[3].Value;
                campo.textoReemplazo = datagrid.Rows[i].Cells[4].Value==null?"":datagrid.Rows[i].Cells[4].Value.ToString();
                campos.Add(campo);
            }
            Campos cam = new Campos(campos.ToArray());
            return cam;
        }

        private void cargarCampos()
        {
            cargarCamposZipCode();
            cargarCamposOrganization();
            cargarCamposContact();
            cargarCamposContactOrganization();
            cargarCamposLocation();
            cargarCamposLocationOrg();
            cargarCamposVolunteerEvent();
            cargarCamposVolunteerTeam();
            cargarCamposVolunteerTeamMember();
            cargarCamposVolunteerOpportunity();
            cargarCamposOccurrence();
            cargarCamposConnection();
            cargarCamposOpportunityOcurrece();
        }
        private void guardarCampos(String archivo, Campos cam, String titulo)
        {
            XmlSerializer serial = new XmlSerializer(typeof(Campos));
            XmlTextWriter xw = new XmlTextWriter(archivo, Encoding.UTF8);
            xw.Formatting = Formatting.Indented;
            xw.Indentation = 1;
            xw.IndentChar = ' ';
            serial.Serialize(xw, cam);
            xw.Close();
            MessageBox.Show("Guardado Completo", titulo, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private void cargarCamposVacios(String[] listado, DataGridView datagrid)
        {
            datagrid.RowCount = listado.Length;
            for (int i = 0; i < listado.Length; i++)
            {
                datagrid.Rows[i].Cells[0].Value = listado[i];
                datagrid.Rows[i].Cells[1].Value = false;
                datagrid.Rows[i].Cells[2].Value = "Texto";
                datagrid.Rows[i].Cells[3].Value = false;
                datagrid.Rows[i].Cells[4].Value = "";
            }
        }
        
        /*
         * Gestion de los ZipCodes
         */
        private void cargarCamposZipCode()
        {
            if (System.IO.File.Exists("ZipCode.xml"))
            {
                cargarCampos("ZipCode.xml", dataGridView1);
            }
            else
            {
                String[] listado = verificar.atributosZipCode();
                cargarCamposVacios(listado,dataGridView1);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            guardarCampos("ZipCode.xml", obtenerCampos(dataGridView1), "Zip Code");
        }
        /*
         * Gestion de los Organization
         */
        private void cargarCamposOrganization()
        {
            if (System.IO.File.Exists("Organization.xml"))
            {
                cargarCampos("Organization.xml", dataGridView2);
            }
            else
            {
                String[] listado = verificar.atributosOrganization();
                cargarCamposVacios(listado, dataGridView2);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            guardarCampos("Organization.xml", obtenerCampos(dataGridView2), "Organization");
        }
        /*
         * Contact
         */
        private void button3_Click(object sender, EventArgs e)
        {
            guardarCampos("Contact.xml",obtenerCampos(dataGridView3),"Contact");
        }
        private void cargarCamposContact()
        {
            if (System.IO.File.Exists("Contact.xml"))
            {
                cargarCampos("Contact.xml", dataGridView3);
            }
            else
            {
                String[] listado = verificar.atributosContact();
                cargarCamposVacios(listado, dataGridView3);
            }
        }
        /*
         * Contact Organization
         */
        private void button4_Click(object sender, EventArgs e)
        {
            guardarCampos("ContactOrganization.xml",obtenerCampos(dataGridView4),"Contact Organization");
        }
        private void cargarCamposContactOrganization()
        {
            if (System.IO.File.Exists("ContactOrganization.xml"))
            {
                cargarCampos("ContactOrganization.xml", dataGridView4);
            }
            else
            {
                String[] listado = verificar.atributosContactOrganization();
                cargarCamposVacios(listado, dataGridView4);
            }
        }
        /*
            Location
         */
        private void button5_Click(object sender, EventArgs e)
        {
            guardarCampos("Location.xml", obtenerCampos(dataGridView5), "Location");
        }
        private void cargarCamposLocation()
        {
            if (System.IO.File.Exists("Location.xml"))
            {
                cargarCampos("Location.xml", dataGridView5);
            }
            else
            {
                String[] listado = verificar.atributosLocation();
                cargarCamposVacios(listado, dataGridView5);
            }
        }
        /*
         * Location Org
         */
        private void button6_Click(object sender, EventArgs e)
        {
            guardarCampos("LocationOrg.xml", obtenerCampos(dataGridView6), "Location Org");
        }
        private void cargarCamposLocationOrg()
        {
            if (System.IO.File.Exists("LocationOrg.xml"))
            {
                cargarCampos("LocationOrg.xml", dataGridView6);
            }
            else
            {
                String[] listado = verificar.atributosLocationOrg();
                cargarCamposVacios(listado, dataGridView6);
            }
        }
        /*
         * Volunteer Event
         */
        private void cargarCamposVolunteerEvent()
        {
            if (System.IO.File.Exists("VolunteerEvent.xml"))
            {
                cargarCampos("VolunteerEvent.xml", dataGridView7);
            }
            else
            {
                String[] listado = verificar.atributosVolunteerEvent();
                cargarCamposVacios(listado, dataGridView7);
            }
        }
        private void button7_Click(object sender, EventArgs e)
        {
            guardarCampos("VolunteerEvent.xml",obtenerCampos(dataGridView7),"Volunteer Event");
        }
        /*
         * Volunteer Team
         */
        private void cargarCamposVolunteerTeam()
        {
            if (System.IO.File.Exists("VolunteerTeam.xml"))
            {
                cargarCampos("VolunteerTeam.xml", dataGridView8);
            }
            else
            {
                String[] listado = verificar.atributosVolunteerTeam();
                cargarCamposVacios(listado, dataGridView8);
            }
        }
        private void button8_Click(object sender, EventArgs e)
        {
            guardarCampos("VolunteerTeam.xml", obtenerCampos(dataGridView8), "Volunteer Team");
        }
        /*
         * Volunteer Team Member
         */
        private void cargarCamposVolunteerTeamMember()
        {
            if (System.IO.File.Exists("VolunteerTeamMember.xml"))
            {
                cargarCampos("VolunteerTeamMember.xml", dataGridView9);
            }
            else
            {
                String[] listado = verificar.atributosVolunteerTeamMember();
                cargarCamposVacios(listado, dataGridView9);
            }
        }
        private void button9_Click(object sender, EventArgs e)
        {
            guardarCampos("VolunteerTeamMember.xml", obtenerCampos(dataGridView9), "Volunteer Team Member");
        }

        /*
         * Volunteer Opportunity
         */
        private void cargarCamposVolunteerOpportunity()
        {
            if (System.IO.File.Exists("VolunteerOpportunity.xml"))
            {
                cargarCampos("VolunteerOpportunity.xml", dataGridView10);
            }
            else
            {
                String[] listado = verificar.atributosVolunteerOpportunity();
                cargarCamposVacios(listado, dataGridView10);
            }
        }
        private void button10_Click(object sender, EventArgs e)
        {
            guardarCampos("VolunteerOpportunity.xml", obtenerCampos(dataGridView10), "Volunteer Opportunity");

        }
        /*
         * Occurrence
         */
        private void cargarCamposOccurrence()
        {
            if (System.IO.File.Exists("Occurrence.xml"))
            {
                cargarCampos("Occurrence.xml", dataGridView11);
            }
            else
            {
                String[] listado = verificar.atributosOccurrence();
                cargarCamposVacios(listado, dataGridView11);
            }
        }
        private void button11_Click(object sender, EventArgs e)
        {
            guardarCampos("Occurrence.xml", obtenerCampos(dataGridView11), "Occurrence");

        }
        /*
         * Connection
         */
        private void cargarCamposConnection()
        {
            if (System.IO.File.Exists("Connection.xml"))
            {
                cargarCampos("Connection.xml", dataGridView12);
            }
            else
            {
                String[] listado = verificar.atributosConnection();
                cargarCamposVacios(listado, dataGridView12);
            }
        }
        private void button12_Click(object sender, EventArgs e)
        {
            guardarCampos("Connection.xml", obtenerCampos(dataGridView12), "Connection");

        }
        /*
         * Opportunity Ocurrence
         */
        private void cargarCamposOpportunityOcurrece()
        {
            if (System.IO.File.Exists("OpportunityOccurrence.xml"))
            {
                cargarCampos("OpportunityOccurrence.xml", dataGridView13);
            }
            else
            {
                String[] listado = verificar.atributosOpportunityOccurrence() ;
                cargarCamposVacios(listado, dataGridView13);
            }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            guardarCampos("OpportunityOccurrence.xml", obtenerCampos(dataGridView12), "Opportunity Occurrence");
        }





        
    }
}
